
/**
 * @Author: 陈瑞鹏
 * @Date:   2022-12-13 19:06:48
 * @Last Modified by:   陈瑞鹏
 * @Last Modified time: 2022-12-15 09:25:56
 */

import { GameFacade } from "../../libs/core/GameFacade";
import { PureMVCEvents } from "../../libs/core/PureMVCEvents";
import { Dictionary } from "../../utils/dictionary";
import exchangeTp from "./exchangeTp";
import treasureEntity from "./treasureEntity";
import treasureTp from "./treasureTp";

export default class treasureCtr {

  private static _ins: treasureCtr = null;

  public static get ins(): treasureCtr {
    if (this._ins == null) {
      this._ins = new treasureCtr();
    }
    return this._ins;
  }
  /**
   * 宝箱模板字典
   */
  public treasureTpDic: Dictionary<any, treasureTp>;

  /**
   * 宝箱兑换字典
   */
  public exchangeTpDic: Dictionary<any, exchangeTp>;

  /**
   * 宝箱数据字典
   */
  public treasureEnyDic: Dictionary<any, treasureEntity>;

  public initTp(tJson, eJson) {
    //宝箱配置
    this.treasureTpDic = new Dictionary();
    for (let val of tJson) {
      if (val) {
        let treasureT = new treasureTp();
        treasureT.init(val);
        this.treasureTpDic.set(treasureT.id, treasureT);
      }
    }

    //宝箱兑换模板配置
    this.exchangeTpDic = new Dictionary();
    for (let val of eJson) {
      if (val) {
        let exchangeT = new exchangeTp();
        exchangeT.init(val);
        this.exchangeTpDic.set(exchangeT.target, exchangeT);
      }
    }
  }

  /**
   * 根据宝箱id获取宝箱模板
   * @param id 
   * @returns 
   */
  public getTreasureTpById(id) {
    return this.treasureTpDic.get(id);
  }

  /**
   * 初始化宝箱数据
   */
  public initData(datas) {
    this.treasureEnyDic = new Dictionary();
    datas.forEach(element => {
      if (element) {
        let treasureEny = new treasureEntity();
        treasureEny.init(element);
        this.treasureEnyDic.set(treasureEny.uid, treasureEny);
      }
    });

    GameFacade.getInstance().sendNotification(PureMVCEvents.ON_TREASURE_UDPATE)
  }
}